
package educar.abmMateria;


import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.swing.JOptionPane;
import educar.models.Materia;

/**
 *
 * @author Marcela Bessone.
 */

public class AgregarMateriaController  implements ActionListener, WindowListener {
   
    private AgregarMateriaView agregarMateriaView;
    private String vector[] = new String[3];
    private MateriaController materiaControl;
    
// <editor-fold defaultstate="collapsed" desc="Constructor">
    
    /**AgregarMateriaController(): Constructor de la clase AgregarMateriaController
     *<hr>
     *<li>pre: true</li>
     *<li>post: crea un objeto de tipo AgregarMateriaController</li>
     *<hr>
     * @param materiaController 
     */
    public AgregarMateriaController(MateriaController materiaController){        
        materiaControl = materiaController;
        agregarMateriaView = new AgregarMateriaView();
        agregarMateriaView.setActionListener(this);
        agregarMateriaView.setWindowListener(this);
    }
 // </editor-fold>
    
// <editor-fold defaultstate="collapsed" desc="Action setView">    
    public void setView() {
        agregarMateriaView.setVisible(true);
    }
// </editor-fold>   
    
// <editor-fold defaultstate="collapsed" desc="Metodos ActionPermormed"> 
    /**
     * 
     * @param e 
     */
    @Override
    public void actionPerformed(ActionEvent e) {
        //Evalua la procedencia del evento
        Object source = e.getSource();
        if (agregarMateriaView.getJbCancelar() == source){
            agregarMateriaView.dispose();
        }
        if (agregarMateriaView.getJbAceptar() == source){
            if (!((agregarMateriaView.getCodmateria().length() == 0) || (agregarMateriaView.getNombre().length() == 0) || (agregarMateriaView.getDNIDocente().length() == 0))) {
                Materia materia = new Materia(agregarMateriaView.getCodmateria(), agregarMateriaView.getNombre(),agregarMateriaView.getDNIDocente());
                if (materia.save()) { 
                            vector[0] = agregarMateriaView.getCodmateria(); 
                            vector[1] = agregarMateriaView.getNombre();
                            vector[2] = agregarMateriaView.getDNIDocente();                             
                            materiaControl.agregarATabla(vector);
                            agregarMateriaView.dispose();
                } else {
                    JOptionPane.showMessageDialog(null,"Ya existe un registro con el codigo de materia "+ agregarMateriaView.getCodmateria()+".","proyecto 2012",JOptionPane.WARNING_MESSAGE);
                }
            } else {
                JOptionPane.showMessageDialog(null,"Existe al menos un campo sin completar.\n\nRevise los campos ingresados","proyecto 2012",JOptionPane.WARNING_MESSAGE);
            }
        }
    }    
// </editor-fold>   
  
// <editor-fold defaultstate="collapsed" desc="Metodos ActionPerformed no usadas">
    @Override
    public void windowOpened(WindowEvent e) {

    }

    @Override
    public void windowClosing(WindowEvent e) {

    }

    @Override
    public void windowClosed(WindowEvent e) {

    }

    @Override
    public void windowIconified(WindowEvent e) {

    }

    @Override
    public void windowDeiconified(WindowEvent e) {

    }

    @Override
    public void windowActivated(WindowEvent e) {

    }

    @Override
    public void windowDeactivated(WindowEvent e) {

    }
// </editor-fold> 


}  //fin clase AgregarMateriaController.